package com.example.handpose_front.HomePage;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.handpose_front.BaseActivity;
import com.example.handpose_front.R;
import com.example.handpose_front.callback.HomeCallBack.IExchangeLoginMode;

public class HomeActivity extends BaseActivity {

    // 一个文本按钮
    private TextView loginTextView;

    // 两个fragment
    private LoginFragment loginFragment = new LoginFragment();
    private VerifyCodeLoginFragment verifyCodeLoginFragment = new VerifyCodeLoginFragment();

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        initView();
    }

    // 初始化控件
    private void initView() {
        // 设置第一个出现的碎片
        replaceFragment(verifyCodeLoginFragment);
        // 设置登录模式切换
        setExchangeMode();
    }

    public void onClick(View v) {
        // 设置切换登录模式功能
        replaceFragment(verifyCodeLoginFragment);
    }

    private void setExchangeMode() {
        // 切换到验证码
        loginFragment.setExchangeLoginMode(new IExchangeLoginMode() {
            @Override
            public void exchangeLoginMode() {
                replaceFragment(verifyCodeLoginFragment);
            }
        });
        // 切换到密码
        verifyCodeLoginFragment.setExchangeLoginMode(new IExchangeLoginMode() {
            @Override
            public void exchangeLoginMode() {
                replaceFragment(loginFragment);
            }
        });

    }

    // 进行碎片之间的切换
    private void replaceFragment(Fragment fragment) {
        // 获取碎片管理类
        FragmentManager fragmentManager = getSupportFragmentManager();
        // 开启碎片事物
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        // 创建事务
        fragmentTransaction.replace(R.id.replaced_by_fragment, fragment);
        // 提交事务
        fragmentTransaction.commit();
    }

}
